package com.hooda.controller;

import com.hooda.pojo.User;
import com.hooda.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @author zhangzp
 * @version 1.0, 10/26/21
 */
@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session) {
        User user = userService.findUser(username);
        if (user != null && password.equals(user.getPassword())) {
            session.setAttribute("user", user);
            return "redirect:/index";
        }
        return "redirect:/toLogin";
    }
}
